package com.ccf.business.source.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ccf.business.source.model.Source;
import com.ccf.business.source.service.ISourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 * 远程服务提供者
 * @author xianggj
 * @Date 2022/2/21 18:24
 **/
@RestController
@RequestMapping("/remote")
@RequiredArgsConstructor
public class RemoteController {

    private final ISourceService fileService;

    /**
     * 避免返回字符串带引号
     * @param sid
     * @return
     */
    @GetMapping(value = "/getViewPath",produces = "text/plain")
    public String getViewPath(@RequestParam(value = "sid", required = true) String sid) {
        try {
            Source detail = fileService.detail(sid);
            return detail.getViewPath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping(value = "/getDataPath",produces = "text/plain")
    public String getDataPath(@RequestParam(value = "sid", required = true) String sid) {
        try {
            Source detail = fileService.getById(sid);
            return detail.getPath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/getDataPathMap")
    public Map<String, String>  getDataPathMap() {
        try {
            List<Source> details = fileService.list(Wrappers.<Source>query()
                    .lambda().select(Source::getId, Source::getPath)
                    .ne(Source::getFileType, Source.FOLDER_TYPE));
            return details.stream()
                    .collect(HashMap::new,(e,v)->e.put(v.getId(),v.getPath()), HashMap::putAll);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping(value = "/getName",produces = "text/plain")
    public String getName(@RequestParam(value = "sid", required = true) String sid) {
        try {
            Source detail = fileService.getById(sid);
            return detail.getName();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/getNameMap")
    public Map<String, String> getNameMap() {
        try {
            List<Source> details = fileService.list(Wrappers.<Source>query()
                    .lambda().select(Source::getId, Source::getName));
            return details.stream()
                    .collect(HashMap::new,(e,v)->e.put(v.getId(),v.getName()), HashMap::putAll);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
